﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PathologyBLL.Service;
using PathologyDAL.Modle;
using PathologyDAL.Service;
using Sunny.UI;

namespace Pathology.UI
{
    public partial class AddFilterCondition : UIForm
    {
        public AddFilterCondition()
        {
            InitializeComponent();
        }

        private Tumour tumour;
        private readonly Action Reload;

        private readonly TumourConditionCanAdd tumourConditionCanAdd;

        public AddFilterCondition(Tumour tumour,Action Reload)
        {
            InitializeComponent();
           
            this.Reload = Reload;
            this.tumour = tumour;
        }

       


        private async void uiComboBoxFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            uiComboBoxName.Text = string.Empty;

            if (uiComboBoxFilter.SelectedIndex>-1)
            {
                ConditionManageServicels conditionManageServicels = conditionManageServicels = new ConditionManageServicels((Filter)uiComboBoxFilter.SelectedIndex);
                var list = await conditionManageServicels.LoadEffictivItems(string.Empty);
                uiComboBoxName.ValueMember = "Id";
                uiComboBoxName.DisplayMember = "Name";
                uiComboBoxName.DataSource = list;
            }
            else
            {
                uiComboBoxName.DataSource = null;

            }

            //从数据库中查
          
          

        }

        private async void uiButtonOK_ClickAsync(object sender, EventArgs e)
        {
            //检查数据
            if (uiComboBoxFilter.SelectedIndex < 0)
            {
                this.ShowErrorDialog2("请先选择类型！");
                return;
            }

            FilterCondition? selectCondition = uiComboBoxName.SelectedItem as FilterCondition;
            if (selectCondition == null)
            {
                this.ShowErrorDialog2("请选择名称");
                return;
            }

            if (uiRadioButtonGroupPossibility.SelectedIndex == -1)
            {
                this.ShowErrorDialog2("请选择概率");
                return;
            }
            //检查数据是否已经存在

            int possibleInt = uiRadioButtonGroupPossibility.SelectedIndex + 1;
            TumourManageServices services = new TumourManageServices();
            bool possibleItemsExist = await services.CheckTumourConditionsExistAsync(tumour, selectCondition);
            if (possibleItemsExist)
            {
                this.ShowErrorDialog2("已经设置过该条目，请设置其他选项！");
                return;
            }
           int res=  await services.AddTumourConditionAsync(tumour, selectCondition, possibleInt);
            if (res> 0) 
            {
                Reload.Invoke();
                
                this.Close();


            }
            else
            {
                this.ShowErrorDialog2("出现错误！！！");
            }


            ////开始保存
            //var tumour = tumourConditionCanAdd.Tumour;
            //TumourManageServices services = new TumourManageServices();
            //;
            //switch (filterCurrent)
            //{
            //    case Filter.BloodVesselFilter:
            //        {
            //            tumour.BloodVessel = selectCondition.Id;
            //            tumour.BloodVesselPossibility = (Possibility)possibleInt;


            //            var res = await services.UpdateConditionAsync(tumour);
            //            if (!res.Success)
            //            {
            //                ShowErrorDialog(res.Message);
            //            }
            //        }

            //        break;
            //    case Filter.CellMorphologyFilter:
            //        {
            //            tumour.CellMorphology = selectCondition.Id;
            //            tumour.CellMorphologyPossibility = (Possibility)possibleInt;
            //            var res = await services.UpdateConditionAsync(tumour);
            //            if (!res.Success)
            //            {
            //                ShowErrorDialog(res.Message);
            //            }
            //        }

            //        break;
            //    case Filter.ImmunohistochemistryFilte:
            //        {
            //            tumour.Immunohistochemistry = selectCondition.Id;
            //            tumour.ImmunohistochemistryPossibility = (Possibility)possibleInt;
            //            var res = await services.UpdateConditionAsync(tumour);
            //            if (!res.Success)
            //            {
            //                ShowErrorDialog(res.Message);
            //            }
            //        }

            //        break;
            //    case Filter.InterstitiumFilter:
            //        {
            //            tumour.Interstitium = selectCondition.Id;
            //            tumour.InterstitiumPossibility = (Possibility)possibleInt;
            //            var res = await services.UpdateConditionAsync(tumour);
            //            if (!res.Success)
            //            {
            //                ShowErrorDialog(res.Message);
            //            }
            //        }

            //        break;
            //    case Filter.OrganizationalStructureFilter:
            //        {
            //            tumour.OrganizationalStructure = selectCondition.Id;
            //            tumour.OrganizationalStructurePossibility = (Possibility)possibleInt;
            //            var res = await services.UpdateConditionAsync(tumour);
            //            if (!res.Success)
            //            {
            //                ShowErrorDialog(res.Message);
            //            }
            //        }

            //        break;
            //    default:
            //        ShowErrorDialog("发生了严重错误");
            //        break;
            //}
            //Reload.Invoke();
            //this.Close();
        }
    }
}
